﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using DG.Tweening;
public class Level_LightLLK : MonoBehaviour
{

    public Level_LightLLKItem item1;


    public List<Level_LightLLKItem> itemList = new List<Level_LightLLKItem>();

    public GameObject lightObj;
    public GameLevelItem glt;

    public bool win = false;

    public void AddItem(Level_LightLLKItem item)
    {
        if (null == item1)
        {
            item1 = item;
        }
        else
        {
            if (item1.id != item.id)
            {
                ResetAllItem();
                SoundManager.Instance.PlayAudio(SoundType.Error.ToString(), false);
            }
            else
            {
                SoundManager.Instance.PlayAudio(SoundType.Click.ToString(), false);
                JudgeResult();
                item1 = null;
            }
        }
    }

    private void ResetAllItem()
    {
        item1 = null;
        int count = itemList.Count;
        for (int i = 0; i < count; i++)
        {
            itemList[i].ResetItem();
        }
    }

    public void JudgeResult()
    {
        if (!win)
        {

            int count = itemList.Count;
            win = true;
            for (int i = 0; i < count; i++)
            {
                if (!itemList[i].colorImage.enabled)
                    win = false;
            }
            if (win)
            {
                for (int i = 0; i < count; i++)
                {
                    itemList[i].transform.DOScale(Vector3.zero, 0.5f);
                }
                Invoke("ShowLight", 0.6f);
            }
            //else
            //{
            //    ResetAllItem();

            //}
        }
    }

    private void ShowLight()
    {
        lightObj.SetActive(true);
        glt.TriggerResult(true);
    }




}
